#include "AnaDef.h"
#include "XGLUtils.h"
#include "style.h"

const Int_t gnch = 6;
const Double_t gnclsNch = 17;

void subdrawTRDeff(const TString fns[], const Int_t nfile, const TString tag="TRD")
{
  SetGlobalStyle(0);
  
  TCanvas *c1=new TCanvas("c1","",600,500);
  PadSetup(c1);
  c1->SetLeftMargin(0.14);
  c1->SetRightMargin(0.01);

  fgkYTitleOffset = 1.4;

  TList *l0=new TList;

  TGraphAsymmErrors* effGrs[]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
  TGraphAsymmErrors* contGrs[]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
  TGraphAsymmErrors* effNclsGrs[]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
  TList *effSigNclsGrs[]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
  const Double_t nclsCut[]={17,18,19,20,21};
  const Int_t ncut = sizeof(nclsCut)/sizeof(Double_t);

  const Int_t col[]={kBlack, kRed, kBlue, kGreen+3, kMagenta,        kBlack, kRed, kBlue, kGreen+3};
  const Int_t fsty[]={3006, 3004,3005, 3021, 3022,                3006, 3004,3005, 3003,};
  const Int_t msty[]={22, 23, 29, 21, 20, 33};

  TString gns[nfile];
  for(Int_t ii=0; ii<nfile; ii++){
    printf("%d: %s\n", ii, fns[ii].Data());

    if(!fns[ii].Contains("dp0.100_")){
      printf("wrong dp!! %s\n", fns[ii].Data()); exit(1);
    }

    if(fns[ii].Contains("mom0.5_"))
      gns[ii]="p = 0.4-0.6 GeV/c";
    else if(fns[ii].Contains("mom1.0_"))
      gns[ii]="p = 0.9-1.1 GeV/c";
    else if(fns[ii].Contains("mom1.5_"))
      gns[ii]="p = 1.4-1.6 GeV/c";
    else if(fns[ii].Contains("mom2.0_"))
      gns[ii]="p = 1.9-2.1 GeV/c";
    else if(fns[ii].Contains("mom3.0_"))
      gns[ii]="p = 2.9-3.1 GeV/c";
    else if(fns[ii].Contains("mom4.0_"))
      gns[ii]="p = 3.9-4.1 GeV/c";
    else{
      printf("wrong mom!");exit(1);
    }
      
    TFile *ff = new TFile(fns[ii]);

    TH1D * hEle = (TH1D*)ff->Get(Form("h%sEle", tag.Data()));
    ResetStyle(hEle);
    TH1D * hPi = (TH1D*)ff->Get(Form("h%sPi", tag.Data()));
    ResetStyle(hPi);
    TH1D *hNclsEle = (TH1D*)ff->Get(Form("h%sNclsEle", tag.Data()));
    ResetStyle(hNclsEle);
    TH1D *hNclsPi = (TH1D*)ff->Get(Form("h%sNclsPi", tag.Data()));
    ResetStyle(hNclsPi);
    TH2D *hSigNclsEle=(TH2D*)ff->Get(Form("h%sSigNclsEle", tag.Data()));
    ResetStyle(hSigNclsEle);
    TH2D *hSigNclsPi=(TH2D*)ff->Get(Form("h%sSigNclsPi", tag.Data()));
    ResetStyle(hSigNclsPi);

    //get eff before normalizing, otherwise the error is wrong!
    effGrs[ii]=XGLUtils::GetPIDEff(hEle, hPi);
    contGrs[ii]=XGLUtils::GetPIDCont(hEle, hPi);
    effNclsGrs[ii]=XGLUtils::GetPIDEff(hNclsEle, hNclsPi);
    effSigNclsGrs[ii]=XGLUtils::Get2DPIDEff(hSigNclsEle, hSigNclsPi, ncut, nclsCut);

    //rebin for high momentum
    if(fns[ii].Contains("mom4.0_")){
      hPi->RebinX(4);
      hEle->RebinX(4);
    }

    hPi->Scale(1/hPi->Integral(0,1000000)/hPi->GetBinWidth(1));
    hEle->Scale(1/hEle->Integral(0,1000000)/hEle->GetBinWidth(1));

    hNclsPi->Scale(1/hNclsPi->Integral(0,1000000));
    hNclsEle->Scale(1/hNclsEle->Integral(0,1000000));

    hPi->GetYaxis()->SetTitle("normalized counts");
    hNclsPi->GetYaxis()->SetTitle("normalized counts");
    hEle->GetYaxis()->SetTitle("normalized counts");
    hNclsEle->GetYaxis()->SetTitle("normalized counts");

    //hPi->GetYaxis()->SetTitle("counts");
    //hNclsPi->GetYaxis()->SetTitle("counts");

    //==== dEdx
    //hPi->SetMaximum(1.2*hPi->GetBinContent(hPi->GetMaximumBin())); 
    //hEle->SetMaximum(1.2*hEle->GetBinContent(hEle->GetMaximumBin()));
    hPi->SetMaximum(3.3);

    hPi->GetXaxis()->SetTitle(tag.Contains("TRD")? "TRD dE/dx+TR (arb. units)":"TPC dE/dx (arb. units)");
    hPi->SetMinimum(0);

    hEle->SetLineColor(kRed);
    hEle->SetFillColor(kRed);
    hEle->SetFillStyle(3004);

    if(tag.Contains("TRD")){
      hPi->GetXaxis()->SetRangeUser(0,4);
    }
    hPi->SetLineColor(kBlue);
    hPi->SetFillColor(kBlue);
    hPi->SetFillStyle(3005);

    TLegend *tmplg = new TLegend(0.85, 0.6, 0.95, 0.88);
    ResetStyle(tmplg, 0.5);
    tmplg->SetTextAlign(33);

    //tmplg->SetTextSize(fgkTextSize*1.2);
    hPi->Draw();
    hEle->Draw("same");
    tmplg->SetHeader(gns[ii]+Form(" N_{ch}=%d N_{cls}/N_{ch}>%.0f (p-Pb #sqrt{s_{NN}}=5.02 TeV)", gnch, gnclsNch));
    tmplg->AddEntry(hEle,"e^{#pm}", "lf");
    tmplg->AddEntry(hPi,"#pi^{#pm}","lf");
    tmplg->Draw();

    TLatex * tmptex1 = new TLatex(0.5, 0.75, "ALICE Performance");
    ResetStyle(tmptex1);
    tmptex1->SetTextSize(0.04);
    tmptex1->Draw();

    TLatex * tmptex = new TLatex(0.5, 0.7, "13/08/2013");
    ResetStyle(tmptex);
    tmptex->SetTextSize(0.04);
    tmptex->Draw();

    c1->Print(Form("elepi%s%d_nclsNch%.0f.eps", tag.Data(), ii, gnclsNch));
    delete tmplg;

    //==== Ncls
    TH1D * hfirst = hNclsEle;

    hfirst->GetXaxis()->SetTitle(tag.Contains("TRD")? "TRD N_{cls}/N_{ch} (arb. units)":"wrong!!");
    hfirst->SetMaximum(1.2*hfirst->GetBinContent(hfirst->GetMaximumBin()));
    hfirst->SetMinimum(0);
    hfirst->GetXaxis()->SetRangeUser(16.5,22.5);

    hNclsEle->SetLineColor(kRed);
    hNclsEle->SetFillColor(kRed);
    hNclsEle->SetFillStyle(3004);

    hNclsPi->SetLineColor(kBlue);
    hNclsPi->SetFillColor(kBlue);
    hNclsPi->SetFillStyle(3005);

    TLegend *tmpnclslg = new TLegend(0.2, 0.6, 0.95, 0.88);
    ResetStyle(tmpnclslg, 0.15);
    //tmpnclslg->SetTextAlign(33);

    //tmpnclslg->SetTextSize(fgkTextSize*1.2);                                                                                                                                                                                                                   
    hNclsEle->Draw();
    hNclsPi->Draw("same");
    tmpnclslg->SetHeader(gns[ii]+Form(" N_{ch}=%d (p-Pb #sqrt{s_{NN}}=5.02 TeV)", gnch));
    tmpnclslg->AddEntry(hNclsEle,"e^{#pm}", "lf");
    tmpnclslg->AddEntry(hNclsPi,"#pi^{#pm}","lf");
    tmpnclslg->Draw();

    c1->Print(Form("Ncls_elepi%s%d_nclsNch%.0f.eps", tag.Data(), ii, gnclsNch));
    delete tmpnclslg;

    //===========

    ResetStyle(effGrs[ii]);
    effGrs[ii]->GetYaxis()->SetRangeUser(9e-4,1);
    effGrs[ii]->GetXaxis()->SetLimits(0.5,1);
    effGrs[ii]->SetLineColor(col[ii]);
    effGrs[ii]->SetMarkerColor(col[ii]);
    effGrs[ii]->SetFillColor(col[ii]);
    effGrs[ii]->SetFillStyle(fsty[ii]);
    effGrs[ii]->SetMarkerStyle(msty[ii]);
    effGrs[ii]->GetXaxis()->SetTitle("electron efficiency");
    effGrs[ii]->GetYaxis()->SetTitle("pion efficiency");

    ResetStyle(contGrs[ii]);
    contGrs[ii]->GetYaxis()->SetRangeUser(5e-3,1);
    contGrs[ii]->GetXaxis()->SetLimits(0.5,1);
    contGrs[ii]->SetLineColor(col[ii]);
    contGrs[ii]->SetMarkerColor(col[ii]);
    contGrs[ii]->SetFillColor(col[ii]);
    contGrs[ii]->SetFillStyle(fsty[ii]);
    contGrs[ii]->SetMarkerStyle(msty[ii]);
    contGrs[ii]->GetXaxis()->SetTitle("electron efficiency");
    contGrs[ii]->GetYaxis()->SetTitle("absolute pion contamination");

    ResetStyle(effNclsGrs[ii]);
    effNclsGrs[ii]->GetYaxis()->SetRangeUser(5e-2,1);
    effNclsGrs[ii]->GetXaxis()->SetLimits(0.5,1);
    effNclsGrs[ii]->SetLineColor(col[ii]);
    effNclsGrs[ii]->SetMarkerColor(col[ii]);
    effNclsGrs[ii]->SetFillColor(col[ii]);
    effNclsGrs[ii]->SetFillStyle(fsty[ii]);
    effNclsGrs[ii]->SetMarkerStyle(msty[ii]);
    effNclsGrs[ii]->GetXaxis()->SetTitle("electron efficiency");
    effNclsGrs[ii]->GetYaxis()->SetTitle("pion efficiency");
  }

  gPad->SetGrid(1,1);
  gPad->SetLogy();

 
  TLegend *lg=new TLegend(0.15, 0.6, 0.45, 0.88);
  ResetStyle(lg);
  //lg->SetFillStyle(1);
  //lg->SetFillColor(kWhite);
  //lg->SetTextSize(fgkTextSize*1.2);
  TString lhead = tag.Contains("TRD")?Form("TRD dE/dx+TR N_{ch}=%d N_{cls}/N_{ch}>%.0f (p-Pb #sqrt{s_{NN}}=5.02 TeV)", gnch, gnclsNch):"TPC dE/dx (p-Pb #sqrt{s_{NN}}=5.02 TeV)";
  lg->SetHeader(lhead);

  //eff
  for(Int_t ii=0; ii<nfile; ii++){
    //effGrs[ii]->Draw(ii?"3l":"a3l");
    effGrs[ii]->SetName(Form("eff%d", ii));effGrs[ii]->SetTitle(gns[ii]);
    effGrs[ii]->Draw(ii?"pl":"apl");l0->Add(effGrs[ii]);
    lg->AddEntry(effGrs[ii], gns[ii], "pl");
  }

  lg->Draw();

  TLatex *tex=new TLatex(0.15, 0.55-0.02,"ALICE Performance");
  ResetStyle(tex);
  tex->Draw();

  TLatex *tex2=new TLatex(0.15, 0.5-0.02,"13/08/2013");
  ResetStyle(tex2);
  tex2->Draw();

  c1->Print(Form("pideff%s_nclsNch%.0f.eps", tag.Data(), gnclsNch));
  c1->Print(Form("pideff%s_nclsNch%.0f.root", tag.Data(), gnclsNch));

  //cont
  for(Int_t ii=0; ii<nfile; ii++){
    contGrs[ii]->SetName(Form("cont%d", ii));contGrs[ii]->SetTitle(gns[ii]);
    contGrs[ii]->Draw(ii?"pl":"apl");l0->Add(contGrs[ii]);
  }

  lg->Draw();

  c1->Print(Form("pidcont%s_nclsNch%.0f.eps", tag.Data(), gnclsNch));

  //Ncls
  gPad->SetLogy(0);

  for(Int_t ii=0; ii<nfile; ii++){
    effNclsGrs[ii]->SetName(Form("effNcls%d", ii));effNclsGrs[ii]->SetTitle(gns[ii]);
    effNclsGrs[ii]->Draw(ii?"pl":"apl");l0->Add(effNclsGrs[ii]);
  }

  lhead = tag.Contains("TRD")?Form("TRD N_{cls}/N_{ch} N_{ch}=%d (p-Pb #sqrt{s_{NN}}=5.02 TeV)", gnch):"TPC dE/dx (p-Pb #sqrt{s_{NN}}=5.02 TeV)";
  lg->SetHeader(lhead);
  lg->Draw();

  c1->Print(Form("Nclseff%s_nclsNch%.0f.eps", tag.Data(), gnclsNch));

  //SigNcls
  gPad->SetLogy(1);

  for(Int_t icut=0; icut<ncut; icut++){
    for(Int_t ifile=0; ifile<nfile; ifile++){
      TGraphAsymmErrors *igr=(TGraphAsymmErrors*)effSigNclsGrs[ifile]->At(icut);
      igr->SetName(Form("file%d%s",ifile, igr->GetName()));
      igr->SetTitle(Form("file%d%s",ifile, igr->GetTitle()));
      ResetStyle(igr);
      igr->GetYaxis()->SetRangeUser(9e-4,1);
      igr->GetXaxis()->SetLimits(0.5,1);
      igr->SetLineColor(col[ifile]);
      igr->SetMarkerColor(col[ifile]);
      igr->SetFillColor(col[ifile]);
      igr->SetFillStyle(fsty[ifile]);
      igr->SetMarkerStyle(msty[ifile]);
      igr->GetXaxis()->SetTitle("electron efficiency");
      igr->GetYaxis()->SetTitle("pion efficiency");

      igr->Draw(ifile?"pl":"apl");l0->Add(igr);
    }

    lhead = tag.Contains("TRD")?Form("TRD dE/dx+TR N_{ch}=%d N_{cls}/N_{ch}>%.0f (p-Pb #sqrt{s_{NN}}=5.02 TeV)", gnch, nclsCut[icut]):"TPC dE/dx (p-Pb #sqrt{s_{NN}}=5.02 TeV)";
    lg->SetHeader(lhead);
    lg->Draw();

    c1->Print(Form("SigNclseff%s_nclsCut%.0f.eps", tag.Data(), nclsCut[icut]));
  }

  //------
  TFile *fout=new TFile("outdrawTRDeff.root","recreate");
  l0->Write();
  fout->Save();
  fout->Close();
  delete fout;

}

void drawTRDeff()
{

  TString fns[10];
  const Double_t mom[]={0.5, 1, 2,3,4};
  const Int_t nf = sizeof(mom)/sizeof(Double_t);

  printf("\nnf: %d\n\n", nf);

  for(Int_t ii=0; ii<nf; ii++){
    fns[ii]= Form("outputneweff_mom%.1f_dp0.100_nch%d_nclsNch%.3f_conv_runlist_LHC13c.pass2.root", mom[ii], gnch, gnclsNch);
    printf("ifile %d/%d: %s\n", ii, nf, fns[ii].Data());
  }

  subdrawTRDeff(fns, nf, "TRD");
  //subdrawTRDeff(fns, nf, "tpc");
}

int main(int argc, char * argv[])
{
  for(Int_t ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }
  if(argc!=1){
    printf("argc!=1\n");return 1;
  }


  drawTRDeff();
  return 0;
}
